package com.Policing;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.Policing.menu.SlideSideMenuExampleActivity;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;

import org.json.JSONException;
import org.json.JSONObject;

import static com.tencent.android.tpush.common.Constants.LogTag;

/**
 * Created by Administrator on 2018/11/7.
 */

public class XG_Receiver extends XGPushBaseReceiver {
    private Intent intent = new Intent("com.qq.xgdemo.activity.UPDATE_LISTVIEW");
    @Override
    public void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {

    }

    @Override
    public void onUnregisterResult(Context context, int i) {

    }

    @Override
    public void onSetTagResult(Context context, int i, String s) {

    }

    @Override
    public void onDeleteTagResult(Context context, int i, String s) {

    }

    @Override
    public void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) {
        // 获取自定义key-value
        String customContent = xgPushTextMessage.getCustomContent();
       // Toast.makeText(context,customContent,Toast.LENGTH_SHORT).show();
        Log.i("1234", customContent);
        if (customContent != null && customContent.length() != 0) {
            try {
                JSONObject obj = new JSONObject(customContent);
                // key1为前台配置的key
                if (!obj.isNull("0001")) {
                 String value = obj.getString("0001");
                    Log.d(LogTag, "get custom value:" + value);
                    //发送广播
                    intent.putExtra("state",value);
                }
                // ...
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult xgPushClickedResult) {

    }

    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {
        String sss= xgPushShowedResult.getContent();
        String xxx = xgPushShowedResult.getTitle();
        Log.i("1111111",xxx);
        Log.i("1111111",sss);
      /*  if (!xxx.equals("")||!sss.equals("")) {
            SharedPreferences userInfo = context.getSharedPreferences("text", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = userInfo.edit();
            editor.putString("sta", xxx);
            editor.commit();    //4.提交数据
        }*/
                    long msgId = xgPushShowedResult.getMsgId();
// 处理推送数据
                     XGPushManager.clearLocalNotifications(context);
 // XGPushManager.deleteTag(context, CommData.TAG_TYPE);
                    NotificationManager notificationManager = (NotificationManager) context
                      .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        notificationManager.cancel((int) msgId);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Intent intent = new Intent(context, SlideSideMenuExampleActivity.class);//将要跳转的界面
        builder.setAutoCancel(true);//点击后消失
        builder.setSmallIcon(R.mipmap.thislogo);//设置通知栏消息标题的头像
        builder.setDefaults(NotificationCompat.DEFAULT_SOUND);//设置通知铃声
        builder.setTicker("xxx");
        builder.setContentText(sss);
      //  builder.setSmallIcon(R.mipmap.thislogo);	//设置通知的小图标，会显示在手机的左上角。
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.thislogo, null);
        builder.setLargeIcon(bitmap);
        builder.setContentTitle(xxx);
       // builder.setLargeIcon(R.mipmap.thislogo);
        //利用PendingIntent来包装我们的intent对象,使其延迟跳转
        PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(intentPend);
        NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());

    }


}
